package com.yyfax.ph.common.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;

public class Utils {

	/**
	 * @param imgPath
	 * 图片路径
	 * @return String
	 */
	public static String encodeBitmapToBase64(String imgPath) {
		Bitmap bitmap = readBitmap(imgPath);
		if (null != bitmap) {
			ByteArrayOutputStream out = null;
			try {
				out = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
				bitmap.recycle();

				out.flush();
				out.close();

				byte[] imgBytes = out.toByteArray();
				return Base64.encodeToString(imgBytes, Base64.DEFAULT);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return null;
			} finally {
				try {
					if (null!=out){
						out.flush();
						out.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			return null;
		}

	}

	/**
	 * @param imgPath
	 * 图片路径
	 * @return Bitmap
	 */
	private static Bitmap readBitmap(String imgPath) {
		try {
			return BitmapFactory.decodeFile(imgPath);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
		}

	}

	/**
	 * @param base64Data
	 * base64数据
	 * @return Bitmap
	 */
	private static Bitmap base64ToBitmap(String base64Data) {
		if (TextUtils.isEmpty(base64Data)) {
			return null;
		} else {
			byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
			return BitmapFactory.decodeByteArray(bytes, 0,
					bytes.length);
		}
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx.xx形式
	 */
	public static String getFormatMoney(String number) {
		String numString = number;
		try {
			if (TextUtils.isEmpty(number)) {
				numString = "0.00";
			} else {
				if (number.contains(".")) {
					if (number.indexOf(".") == number.length() - 1) {//XXXX.
						numString = getDecoratedString(number.substring(0, number.length() - 1)) + ".00";
					} else {
						double bg = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						String[] nums = String.valueOf(bg).split("\\.");
						if (nums[1].length() == 1) {
							nums[1] += "0";
						}
						numString = getDecoratedString(nums[0]) + "." + nums[1];
					}
				} else {
					numString = getDecoratedString(number) + ".00";//XXXX

				}
			}
		} catch (Exception e) {
		}
		return numString;
	}

	public static String getDecoratedString(String value) {
		if (value.length() <= 3) return value;
		String numberPattern = "###,###,###";
		DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
		formatter.applyPattern(numberPattern);
		String result = formatter.format(Integer.parseInt(value));
		return result;
	}

	// 根据Unicode编码判断中文汉字和符号
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	// 判断中文汉字和符号
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}

	public static <T> boolean notEmpty(List<T> list) {
		return !isEmpty(list);
	}

	public static <T> boolean isEmpty(List<T> list) {
		if (list == null || list.size() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 检查是否已安装某app
	 *
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean checkApkExist(Context context, String packageName) {
		if (TextUtils.isEmpty(packageName)) {
			return false;
		}
		try {
			context.getPackageManager().getApplicationInfo(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES);
			return true;
		} catch (PackageManager.NameNotFoundException e) {
			return false;
		}
	}
}
